from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver import ChromeOptions

# 无头浏览器设置
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
# 规避检查
options = ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-automation'])

bro = webdriver.Chrome(executable_path="./chromedriver.exe", chrome_options=chrome_options, options=options)

# ============================= #
#           爬取车销量
# ============================= #

with open("data/brand.txt", "r", encoding="utf-8") as lines:
    for line in lines:
        print(line.split(",")[0])

        bro.get("https://price.pcauto.com.cn/salescar/" + line.split(",")[0].split("/")[-2] + "/")

        try:
            table = bro.find_element_by_tag_name("tbody")
            content = table.text
            trs = table.find_elements_by_tag_name("tr")
            for tr in trs:
                list = []
                list.append(line.split(",")[0].split("/")[-2])
                if tr.get_attribute("class") == "":
                    continue
                for td in tr.find_elements_by_tag_name("td"):
                    list.append(td.text)
                print(list)

                with open(
                        "data/sale.csv",
                        "a", encoding="utf-8") as file:
                    file.write(",".join(list) + "\n")

            # with open(
            #         "data/sale/" + line.split(",")[0].split("/")[-2] + "_" + line.split(",")[1].split(" ")[0] + ".txt",
            #         "w", encoding="utf-8") as file:
            #     file.write(content)
        except:
            pass
